package cn.zd.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Toast;

import cn.zd.demo1.adapter.MyListViewBaseAdapter;
import cn.zd.demo1.util.DisplayUtil;

public class ListViewActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, CompoundButton.OnCheckedChangeListener {

    private MyListViewBaseAdapter adapter_1;
    private CheckBox ck_line;
    private CheckBox ck_bg;
    private ListView lv_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Toast.makeText(this,this.getClass().getName(),Toast.LENGTH_LONG).show();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);

        ck_line = findViewById(R.id.ck_line);
        ck_bg = findViewById(R.id.ck_bg);
        ck_line.setOnCheckedChangeListener(this);
        ck_bg.setOnCheckedChangeListener(this);

        lv_1 = findViewById(R.id.lv_1);
        adapter_1 = new MyListViewBaseAdapter(this);
        lv_1.setAdapter(adapter_1);
        lv_1.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this,"通过列表你选择的是：" + adapter_1.getData(position).getName(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.getId() ==R.id.ck_line) {

            if (isChecked) {
                Drawable drawable = getResources().getDrawable(R.color.black,getTheme());
                lv_1.setDivider(drawable);
                lv_1.setDividerHeight(DisplayUtil.dp2px(this,1));
            } else {
                lv_1.setDivider(null);
                lv_1.setDividerHeight(0);
            }

        }else if (buttonView.getId() == R.id.ck_bg) {
            if (isChecked) {
                Drawable drawable = getResources().getDrawable(R.color.black,getTheme());
                lv_1.setSelector(R.drawable.selector_listview);
            } else {
                lv_1.setSelector(getResources().getDrawable(R.color.transparent,getTheme()));
            }
        }
    }
}